﻿using System;
using System.Collections.Generic;
using System.Text;
using X.DataFactory;
using System.Data;
using X.Entity;

namespace X.Data.Access
{
    public class User : IUser
    {
        static UserInfo UserFromReader(NullableDataReader reader)
        {
            UserInfo userInfo = new UserInfo();

            userInfo.UserId = reader.GetInt32("UserId");
            userInfo.LoginName = reader.GetString("LoginName");
            userInfo.Password = reader.GetValue("Password") as byte[];
            userInfo.UserName = reader.GetString("UserName");

            return userInfo;
        }

        public bool ChangePassword(int userId, byte[] password)
        {
            string sSQL = "UPDATE X_User SET Password = @Password WHERE UserId = @UserId";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Password", DbType.Binary, password);
            cmdParams.AddInParameter("@UserId", DbType.Int32, userId);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public bool Create(Entity.UserInfo userInfo)
        {
            string strSql = @"INSERT INTO [X_User]([LoginName],[Password],[UserName])
                              VALUES(@LoginName,@Password,@UserName)";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@LoginName", DbType.String, userInfo.LoginName);
            cmdParams.AddInParameter("@Password", DbType.Binary, userInfo.Password);
            cmdParams.AddInParameter("@UserName", DbType.String, userInfo.UserName);

            return DBHelper.ExecuteSql(strSql, cmdParams);
        }

        public bool Delete(int userId)
        {
            string sSQL = "DELETE FROM X_User WHERE LoginName <> 'Admin' AND UserId = @UserId";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@UserId", DbType.Int32, userId);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public bool Exists(string loginName)
        {
            string strSql = "SELECT COUNT(*) FROM X_User WHERE LoginName = @LoginName";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@LoginName", DbType.String, loginName);

            return DBHelper.ExistsSql(strSql, cmdParams);
        }

        public Entity.UserInfo GetUser(int userId)
        {
            string sSQL = "SELECT * FROM X_User WHERE UserId = @UserId";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@UserId", DbType.Int32, userId);

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL, cmdParams))
            {
                while (reader.Read())
                {
                    return UserFromReader(reader);
                }
            }

            return new UserInfo(true);
        }

        public Entity.UserInfo GetUser(string loginName)
        {
            string strSql = "SELECT UserId,LoginName,Password,UserName FROM X_User WHERE LoginName = @LoginName";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@LoginName", DbType.String, loginName);

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(strSql, cmdParams))
            {
                if (reader.Read())
                {
                    return UserFromReader(reader);
                }
            }

            return new UserInfo(true);
        }

        public IList<Entity.UserInfo> GetUserList()
        {
            string sSQL = "SELECT * FROM X_User";

            IList<Entity.UserInfo> list = new List<Entity.UserInfo>();

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL))
            {
                while (reader.Read())
                {
                    list.Add(UserFromReader(reader));
                }
            }

            return list;
        }

        public bool Update(Entity.UserInfo user)
        {
            string sSQL = "UPDATE X_User SET UserName = @UserName WHERE UserId = @UserId";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@UserName", DbType.String, user.UserName);
            cmdParams.AddInParameter("@UserId", DbType.Int32, user.UserId);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }
    }
}
